package com.mes.task.service;


import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.provider.Page;
import com.mes.common.service.BasicService;
import com.mes.common.util.SysUtil;
import com.mes.invented.dao.VPartDao;
import com.mes.invented.doman.VPart;
import com.mes.task.dao.DemandDao;
import com.mes.task.dao.PartTaskDao;
import com.mes.task.dao.TaskDao;
import com.mes.task.domain.Demand;
import com.mes.task.domain.PartTask;
import com.mes.task.domain.Task;
import com.mes.task.manager.DemandManager;
import com.mes.util.ScheduleUtil;

@Service
public class DemandService extends BasicService<Demand> {

	@Resource
	private DemandManager demandManager;
	
	@Resource
	private ScheduleUtil scheduleUtil;
	
	@Resource
	private TaskDao taskDao;
	
	@Resource
	private VPartDao vpartDao;
	
	@Resource
	private DemandDao demandDao;
	
	@Resource
	private SysUtil sysUtil;
	
	@Resource
	private PartTaskDao partTaskDao;
	
	@DataProvider
	public void getProductDemands(Map<String, Object> params,Page<Demand> page) throws Exception {

		String parentDemandUid = (String) params.get("parentDemandUid");
		List<Demand> list = demandManager.getProductDemandsByDrawingIdContignationNumber(parentDemandUid,
				params, page);
		if (list != null) {
			if (parentDemandUid.equalsIgnoreCase("null"))
				scheduleUtil.initPage(page, list);
			else {
				page = null;
			}
		}

	}
	
	/**
	 * @author Snowbear
	 * @see 获取登录人员的id和角色信息
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Expose
	public Map<String, Object> initAssembleDemandQueryView(Map<String, Object> params) throws Exception {
		
		Map<String, Object> map = new HashMap<>();
		String employeeId = sysUtil.getSessionEmployeeUid();
		List<String> roleId = sysUtil.getSessionRoleId();
		map.put("employeeId", employeeId);
		map.put("roleId", roleId);

		return map;
	}
	
	/**
	 * 通过DrawingIdBatchnumTaskManagerUid查找到相关task任务
	 * @param params
	 * @param page
	 */
	@DataProvider
	public void getTasksByDrawingIdBatchnumTaskManagerUid(Map<String, Object> params,Page<Task> page) {
		
		String drawingId = (String) params.get("drawingId");
		String batchnum = (String) params.get("batchnum");
		String taskManagerUid = (String) params.get("taskManagerUid");
		List<Task> list = new ArrayList<>();
		Session session = taskDao.getSessionFactory().getCurrentSession();
		String sql = "select t.*"
				+ " from t_tasks t"
				+ " inner join b_part_number bp on bp.part_number = t.part_number"
				+ " where t.tasktype like '109%' and (t.taskstate is null or t.taskstate < '106')"
				+ " and upper(bp.drawingid) like upper(replace('"+ drawingId +"', '*', '%'))"
				+ " and upper(t.batchnum) like upper(replace('"+ batchnum +"', '*', '%'))"
				+ " and t.task_manager like replace('"+ taskManagerUid +"', '*', '%')"
				+ " and parentuid is null"
				+ " order by t.createtime desc, bp.drawingid, t.batchnum";
		list = session.createSQLQuery(sql).addEntity(Task.class).list();
		scheduleUtil.initPage(page, list);
	}
	
/*	*//**
 * 暂时有点问题
	 * @throws Exception 
	 * 
	 *//*
	@DataProvider
	public Collection<PartTask> getPartTasksByTaskUid(String taskUid) throws Exception{
		
		List<PartTask> list = new ArrayList<>();
		list = partTaskDao.findByProperty("taskUid", taskUid);
		return list;
	}*/
	
	
	/**
	 * 通过parentuid获取零件任务parttask
	 * 应该还有taskUid来获取参数
	 * @param params
	 * @return
	 */
	@DataProvider
	public Collection<PartTask> getPartTasksByParentuid(Map<String, Object> params) {
		String parentuid = (String) params.get("parentUid");
		List<PartTask> list = new ArrayList<>();
		Session session = taskDao.getSessionFactory().getCurrentSession();
		String sql = "select t.* from t_tasks t"
				+ " inner join b_part_number bp on bp.part_number = t.part_number"
				+ " where parentuid = '"+ parentuid +"' and t.tasktype = '110'"
				+ " order by bp.drawingid";
		list = session.createSQLQuery(sql).addEntity(PartTask.class).list();
		return list;
	}
	
	/**
	 * 获取架次计划界面的Demand
	 * @author cswen
	 * @param page
	 * @param parameters
	 * @throws Exception
	 */
	@DataProvider
	public void getAssembleAODemand(Page<Demand> page, Map<String, Object> parameters) throws Exception {
		String partUid = (String) parameters.get("partUid");
		String batchNum = (String) parameters.get("batchNum");
		String model = (String) parameters.get("model");
		String drawingId = (String) parameters.get("drawingId");
		List<Demand> productDemandList = this.demandDao.getAssembleAODemand(
				partUid, model,drawingId,batchNum, page);
		if (productDemandList != null && productDemandList.size() > 0) {
			scheduleUtil.initPage(page, productDemandList);;
		}
	}
	
	/**
	 * 保存架次计划界面的Demand
	 * @author cswen
	 * @param demandList
	 * @throws Exception
	 */
	@DataResolver
	@Transactional
	public void saveAssembleAODemand(Collection<Demand> demandList) throws Exception {
		saveAll(demandList);
	}
	
	/**
	 * 分解Demand
	 * @author cswen
	 * @param demandList
	 * @throws Exception
	 */
	@DataResolver
	@Transactional
	public void breakDemand(Collection<Demand> demandList)
			throws Exception {
		for (Demand demand : demandList) {
			if (demand.getIsBreak() != null && !demand.getIsBreak()) {
				demandManager.aoBreak(demand);
			}
		}
	}
	
	@DataProvider
	public void getVpartList(Page<VPart> page, Map<String, Object> parameters) throws Exception {
		String drawingId = (String) parameters.get("drawingId");
		String part_name = (String) parameters.get("part_name");
		String typeParameter = (String) parameters.get("type");
		List<VPart> vpartList = vpartDao.getVPartList(typeParameter, drawingId, part_name);
		if (vpartList != null && vpartList.size() > 0) {
			scheduleUtil.initPage(page, vpartList);;
		}
	}

}
